package com.spzx.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.spzx.cart.api.domain.CartInfo;
import com.spzx.common.core.context.SecurityContextHolder;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresLogin;
import com.spzx.service.ICartService;
import io.jsonwebtoken.impl.Base64Codec;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

/**
 * ClassName: CartController
 * Package: com.spzx.controller
 * Description:
 */
@Tag(name = "购物车")
@RestController
@RequiresLogin
public class CartController extends BaseController {
    @Autowired
    private ICartService cartService;

    @Operation(summary = "添加购物车")
    @GetMapping("/addToCart/{skuId}/{skuNum}")
    public AjaxResult addToCart(@PathVariable("skuId") Long skuId, @PathVariable("skuNum") Integer skuNum){
        cartService.addToCart(skuId, skuNum);
        return success();
    }

    @Operation(summary = "获取购物车列表")
    @GetMapping("/cartList")
    public AjaxResult getCartList(){
        Long userId = SecurityContextHolder.getUserId();
        List<CartInfo> cartInfoList = cartService.getCartList(userId);
        return success(cartInfoList);
    }

    @Operation(summary = "删除购物车中的商品")
    @DeleteMapping("/deleteCart/{skuId}")
    public AjaxResult deleteCart(@PathVariable("skuId") List<Long> skuIds){
        Long userId = SecurityContextHolder.getUserId();
        return toAjax(cartService.deleteCart(skuIds,userId).intValue());
    }

    @Operation(summary = "更改选择状态")
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public AjaxResult changeChecked(@PathVariable("skuId") Long skuId,@PathVariable("isChecked") Integer isChecked){
        Long userId = SecurityContextHolder.getUserId();
        return toAjax(cartService.changeChecked(skuId,isChecked,userId));
    }

    @Operation(summary = "全选操作")
    @GetMapping("/allCheckCart/{isChecked}")
    public AjaxResult allCheckCart(@PathVariable("isChecked") Integer isChecked){
        Long userId = SecurityContextHolder.getUserId();
        return toAjax(cartService.allCheckCart(isChecked,userId));
    }

    @Operation(summary = "清空购物车")
    @GetMapping("/clearCart")
    public AjaxResult clearCart(){
        Long userId = SecurityContextHolder.getUserId();
        return toAjax(cartService.clearCart(userId));
    }

    @Operation(summary = "更新购物车价格")
    @PostMapping("/updateCartPrice")
    @InnerAuth
    public R updateCartPrice(){
        Long userId = SecurityContextHolder.getUserId();
        cartService.updateCartPrice(userId);
        return R.ok();

    }

    @InnerAuth
    @Operation(summary = "获取选中的商品")
    @GetMapping("/getCheckList/{userId}")
    public R<List<CartInfo>> getCheckList(@PathVariable("userId") Long userId){
        return R.ok(cartService.getCheckList(userId));
    }
}
